package com.epalmpay.service.activity.impl;

import javax.annotation.Resource;

import com.epalmpay.dto.activity.ActivityRoutineInDTO;
import com.epalmpay.dto.activity.ActivityRoutineListOutDTO;
import com.epalmpay.dto.activity.ActivityRoutineOutDTO;
import com.epalmpay.dto.agentmanage.TradeWithdrawOutDTO;
import com.epalmpay.entity.*;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.mapper.ActivityBaseMapper;
import com.epalmpay.mapper.ActivityRoutineRegisterMapper;
import com.epalmpay.mapper.ActivityRoutinesignedMapper;
import com.epalmpay.util.Tools;
import org.springframework.stereotype.Service;

import com.epalmpay.dto.activity.MemberBirthdayInDTO;
import com.epalmpay.mapper.ActivityRoutineMapper;
import com.epalmpay.service.activity.IActivityRoutineService;

import java.util.*;

/**
 * Created by Administrator on 2017/5/22.
 */
@Service("activityRoutineService")
public class ActivityRoutineServiceImpl implements IActivityRoutineService{
    @Resource
    private ActivityRoutineMapper activityRoutineMapper;

    @Resource
    private ActivityBaseMapper activityBaseMapper;

    @Resource
    ActivityRoutinesignedMapper activityRoutinesignedMapper;

    @Resource
    ActivityRoutineRegisterMapper activityRoutineRegisterMapper;




    @Override
    public ActivityRoutineListOutDTO GetActivityRoutineList(ActivityRoutineInDTO activityRoutineInDTO, Page page) throws Exception {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", activityRoutineInDTO.getGroupId());
        param.put("startDate", activityRoutineInDTO.getStartDate());
        param.put("endDate", activityRoutineInDTO.getEndDate());
        param.put("name", activityRoutineInDTO.getName());
        param.put("activityType", activityRoutineInDTO.getActivityType());
        param.put("id", activityRoutineInDTO.getGroupId()*100+30+activityRoutineInDTO.getActivityType());


        List<ActivityRoutineOutDTO> result = new ArrayList<ActivityRoutineOutDTO>();
        int num=activityRoutineMapper.selectActivitySpecialRoutineListCount(param);
        page.setTotalResult(num);

        param.put("limit", page.getShowCount());
        if (page.getCurrentPage()>0){
            param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
        }else{
            param.put("start", page.getShowCount()*(0));
        }



        List<Map<String,Object>> activitySpecialRoutineList = activityRoutineMapper.selectActivitySpecialRoutineList(param);

        for (Map<String, Object> tempObject : activitySpecialRoutineList) {
            ActivityRoutineOutDTO activityRoutineOutDTO = new ActivityRoutineOutDTO();
            activityRoutineOutDTO.setGmtCreate((Date)tempObject.get("gmt_create"));
            activityRoutineOutDTO.setMemberGradeName((String)tempObject.get("grade_name"));
            activityRoutineOutDTO.setMemberName((String)tempObject.get("member_name"));
            activityRoutineOutDTO.setBirthdayDate((Date)tempObject.get("birthday"));
            activityRoutineOutDTO.setCollectionTime((Date)tempObject.get("get_time"));
            activityRoutineOutDTO.setFromName((String)tempObject.get("from_name"));
            activityRoutineOutDTO.setFromType((Integer) tempObject.get("from_type"));
//            activityRoutineOutDTO.setSendType((Integer) tempObject.get("send_type"));
            activityRoutineOutDTO.setSendName((String) tempObject.get("send_name"));
            activityRoutineOutDTO.setActivityType((Integer) tempObject.get("activity_type"));
            activityRoutineOutDTO.setStatus((Integer) tempObject.get("STATUS"));
            activityRoutineOutDTO.setCouponId((Integer) tempObject.get("coupon_id"));
            activityRoutineOutDTO.setRedpackageId((Integer) tempObject.get("redpackage_id"));
            activityRoutineOutDTO.setIntegral((Integer) tempObject.get("integral"));
            if (!Tools.isEmpty(tempObject.get("send_type")+"")) {
            Integer type=Integer.parseInt(tempObject.get("send_type")+"");
                activityRoutineOutDTO.setSendType(type);
            }
            activityRoutineOutDTO.setRemark((String) tempObject.get("remark"));



            result.add(activityRoutineOutDTO);
        }
        ActivityRoutineListOutDTO activityRoutineListOutDTO=new ActivityRoutineListOutDTO();
        ActivityRoutine activityRoutine=activityRoutineMapper.selectByPrimaryKeyLeftJonhBaseDate(param);
        activityRoutineListOutDTO.setActivityRoutineOutDTOList(result);
        activityRoutineListOutDTO.setPage(page);
        activityRoutineListOutDTO.setStatus(activityRoutine.getStatus()+"");
        return activityRoutineListOutDTO;
    }

    @Override
    public int UpdateRoutineWithGroupIdAndActivityId(ActivityRoutine activityRoutine) {

        int status =activityRoutineMapper.updateSpecialActivityByPrimaryKey(activityRoutine);
        return status;
    }

    @Override
    public ActivityRoutine GetRoutineWithGroupIdAndActivityId(MemberBirthdayInDTO memberBirthdayInDTO) {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", memberBirthdayInDTO.getGroupId());
        param.put("id", memberBirthdayInDTO.getId());

        ActivityRoutine activityRoutine=activityRoutineMapper.selectByPrimaryKeyLeftJonhBaseDate(param);

        return activityRoutine;
    }

    @Override
    public ActivityRoutine InitialRoutineActivity(ActivityRoutine activityRoutine) throws Exception {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId", activityRoutine.getGroupId());
//        查询结果
        Boolean queryResult=true;

        List<ActivityRoutine> result = new ArrayList<ActivityRoutine>();

        ActivityRoutinesigned activityRoutinesigned=activityRoutinesignedMapper.selectByPrimaryKeyLeftJonhBaseData((activityRoutine.getGroupId()*100+35l));
        if (activityRoutinesigned==null){
            ActivityRoutinesigned activityRoutinesigned1=new ActivityRoutinesigned();
            activityRoutinesigned1.setActivityId((activityRoutine.getGroupId()*100+35l));
            activityRoutinesigned1.setActivityType(5);
            activityRoutinesigned1.setSecondSendType(ActivityEnum.ActivitySendType.NoSend.getType());
            activityRoutinesigned1.setThirdSendType(ActivityEnum.ActivitySendType.NoSend.getType());
            activityRoutinesigned1.setGmtCreate(new Date());
            activityRoutinesigned1.setMemberRiented(1);
            activityRoutinesigned1.setSignedTimeType(1);
            activityRoutinesignedMapper.insert(activityRoutinesigned1);
        }

        ActivityRoutineRegister activityRoutineRegister=activityRoutineRegisterMapper.selectByPrimaryKey((activityRoutine.getGroupId()*100+36l));
        if (activityRoutineRegister==null){
            ActivityRoutineRegister activityRoutineRegister1=new ActivityRoutineRegister();
            activityRoutineRegister1.setActivityId((activityRoutine.getGroupId()*100+36l));
            activityRoutineRegister1.setActivityType(6);
            activityRoutineRegister1.setGmtCreate(new Date());
            activityRoutineRegister1.setMemberRiented(1);
            activityRoutineRegister1.setFirstSendType(ActivityEnum.ActivitySendType.NoSend.getType());
            activityRoutineRegister1.setSecondSendType(ActivityEnum.ActivitySendType.NoSend.getType());
            activityRoutineRegister1.setFirstSendType(ActivityEnum.ActivitySendType.NoSend.getType());
            activityRoutineRegister1.setInviteeSendType(ActivityEnum.ActivitySendType.NoSend.getType());
            activityRoutineRegister1.setInviterSendType(ActivityEnum.ActivitySendType.NoSend.getType());
            activityRoutineRegisterMapper.insert(activityRoutineRegister1);
        }
//        根据groupId查询所有的固定活动数量
       int num= activityRoutineMapper.selectActivityRoutineListCount(param);
        if (num!=7){
            queryResult=false;
        }
//根据groupId查询所有的固定活动,默认数据acitivtyid 31-37
        List<Map<String,Object>> routineActivityOutDTOList = activityRoutineMapper.selectActivityRoutineList(param);

            for (int i = 0; i <num ; i++) {
                Map<String, Object> tempObject=routineActivityOutDTOList.get(i);
                ActivityRoutine activityRoutine1=new ActivityRoutine();
                activityRoutine1.setActivityId((long)tempObject.get("activity_id"));
                activityRoutine1.setActivityType((Integer) tempObject.get("activity_type"));
                if (0==i){
                    if (((activityRoutine.getGroupId()*100+31l)!=activityRoutine1.getActivityId())||(activityRoutine1.getActivityType()!= ActivityEnum.RoutineActivityType.BirthDaySend.getType())){
                        queryResult=false;
                    }
                }
                if (1==i){
                    if (((activityRoutine.getGroupId()*100+32l)!=activityRoutine1.getActivityId())||(activityRoutine1.getActivityType()!= ActivityEnum.RoutineActivityType.FixedTime.getType())){
                        queryResult=false;
                    }
                }
                if (2==i){
                    if (((activityRoutine.getGroupId()*100+33l)!=activityRoutine1.getActivityId())||(activityRoutine1.getActivityType()!= ActivityEnum.RoutineActivityType.UpdateSend.getType())){
                        queryResult=false;
                    }
                }
                if (3==i){
                    if (((activityRoutine.getGroupId()*100+34l)!=activityRoutine1.getActivityId())||(activityRoutine1.getActivityType()!= ActivityEnum.RoutineActivityType.MemberLifeCycle.getType())){
                        queryResult=false;
                    }
                }
                if (4==i){
                    if (((activityRoutine.getGroupId()*100+35l)!=activityRoutine1.getActivityId())||(activityRoutine1.getActivityType()!= ActivityEnum.RoutineActivityType.MemberSigned.getType())){
                        queryResult=false;
                    }
                }
                if (5==i){
                    if (((activityRoutine.getGroupId()*100+36l)!=activityRoutine1.getActivityId())||(activityRoutine1.getActivityType()!= ActivityEnum.RoutineActivityType.InviteRegistration.getType())){
                        queryResult=false;
                    }
                }
                if (6==i){
                    if (((activityRoutine.getGroupId()*100+37l)!=activityRoutine1.getActivityId())||(activityRoutine1.getActivityType()!= ActivityEnum.RoutineActivityType.ComsumeShare.getType())){
                        queryResult=false;
                    }
                }
                result.add(activityRoutine1);
            }

//如果数据有问题，删除重新创建
        if (queryResult==false){

            for (int i = 0; i <7 ; i++) {

                if (i<num){
                    ActivityRoutine activityRoutine2=result.get(i);
                    activityRoutineMapper.deleteByPrimaryKey(activityRoutine2.getActivityId());
//                    activityBaseMapper.deleteByActivityId(activityRoutine2.getActivityId());
                }

                if (0==i){
                    ActivityRoutine activityRoutine1=new ActivityRoutine();
                    activityRoutine1.setActivityId(activityRoutine.getGroupId()*100+31l);
                    activityRoutine1.setActivityType(ActivityEnum.RoutineActivityType.BirthDaySend.getType());
                    activityRoutine1.setGmtCreate(new Date());
                    activityRoutine1.setGmtModified(new Date());
                    activityRoutine1.setMemberRiented(1);//默认每个会员，(1,2)
                    activityRoutine1.setSendTime(0);

                    activityRoutine1.setGroupId(activityRoutine.getGroupId());
                    activityRoutineMapper.deleteByPrimaryKey(activityRoutine1.getActivityId());
                    activityRoutineMapper.insert(activityRoutine1);
                    ActivityBase activityBase=new ActivityBase();
                    activityBase.setActivityId(activityRoutine.getGroupId()*100+31l);
                    activityBase.setCreateUserId(activityRoutine.getLoginId());
                    activityBase.setGmtCreate(new Date());
                    activityBase.setActivityName("会员生日活动");
                    activityBase.setActivityType(1);
                    activityBase.setStatus(0);
                    activityBase.setGroupId(activityRoutine.getGroupId());
                    ActivityBase activityBase1=activityBaseMapper.selectByRoutineId(activityBase.getActivityId());
                    if(activityBase1!=null){
                        activityBaseMapper.updateByPrimaryKeyActivityId(activityBase);
                    }else{
                        activityBaseMapper.insert(activityBase);
                    }


                }
                if (1==i){
                    ActivityRoutine activityRoutine1=new ActivityRoutine();
                    activityRoutine1.setActivityId(activityRoutine.getGroupId()*100+32l);
                    activityRoutine1.setActivityType(ActivityEnum.RoutineActivityType.FixedTime.getType());
                    activityRoutine1.setGmtCreate(new Date());
                    activityRoutine1.setGmtModified(new Date());
                    activityRoutine1.setMemberRiented(1);//默认每个会员，(1,2)
                    activityRoutine1.setSendTime(0);
                    activityRoutine1.setGroupId(activityRoutine.getGroupId());
                    activityRoutineMapper.deleteByPrimaryKey(activityRoutine1.getActivityId());
                    activityRoutineMapper.insert(activityRoutine1);

                    ActivityBase activityBase=new ActivityBase();
                    activityBase.setActivityId(activityRoutine.getGroupId()*100+32l);
                    activityBase.setCreateUserId(activityRoutine.getLoginId());
                    activityBase.setGmtCreate(new Date());
                    activityBase.setActivityName("固定时间活动");
                    activityBase.setActivityType(1);
                    activityBase.setStatus(0);
                    activityBase.setGroupId(activityRoutine.getGroupId());
                    ActivityBase activityBase1=activityBaseMapper.selectByRoutineId(activityBase.getActivityId());
                    if(activityBase1!=null){
                        activityBaseMapper.updateByPrimaryKeyActivityId(activityBase);
                    }else{
                        activityBaseMapper.insert(activityBase);
                    }

                }

                if (2==i){
                    ActivityRoutine activityRoutine1=new ActivityRoutine();
                    activityRoutine1.setActivityId(activityRoutine.getGroupId()*100+33l);
                    activityRoutine1.setActivityType(ActivityEnum.RoutineActivityType.UpdateSend.getType());
                    activityRoutine1.setGmtCreate(new Date());
                    activityRoutine1.setGmtModified(new Date());
                    activityRoutine1.setMemberRiented(1);//默认每个会员，(1,2)
                    activityRoutine1.setSendTime(0);
                    activityRoutine1.setGroupId(activityRoutine.getGroupId());
                    activityRoutineMapper.deleteByPrimaryKey(activityRoutine1.getActivityId());
                    activityRoutineMapper.insert(activityRoutine1);

                    ActivityBase activityBase=new ActivityBase();
                    activityBase.setActivityId(activityRoutine.getGroupId()*100+33l);
                    activityBase.setCreateUserId(activityRoutine.getLoginId());
                    activityBase.setGmtCreate(new Date());
                    activityBase.setActivityName("升级赠送活动");
                    activityBase.setActivityType(1);
                    activityBase.setStatus(0);
                    activityBase.setGroupId(activityRoutine.getGroupId());
                    ActivityBase activityBase1=activityBaseMapper.selectByRoutineId(activityBase.getActivityId());
                    if(activityBase1!=null){
                        activityBaseMapper.updateByPrimaryKeyActivityId(activityBase);
                    }else{
                        activityBaseMapper.insert(activityBase);
                    }

                }

                if (3==i){
                    ActivityRoutine activityRoutine1=new ActivityRoutine();
                    activityRoutine1.setActivityId(activityRoutine.getGroupId()*100+34l);
                    activityRoutine1.setActivityType(ActivityEnum.RoutineActivityType.MemberLifeCycle.getType());
                    activityRoutine1.setGmtCreate(new Date());
                    activityRoutine1.setGmtModified(new Date());
                    activityRoutine1.setMemberRiented(1);//默认每个会员，(1,2)
                    activityRoutine1.setSendTime(0);
                    activityRoutine1.setGroupId(activityRoutine.getGroupId());
                    activityRoutineMapper.deleteByPrimaryKey(activityRoutine1.getActivityId());
                    activityRoutineMapper.insert(activityRoutine1);

                    ActivityBase activityBase=new ActivityBase();
                    activityBase.setActivityId(activityRoutine.getGroupId()*100+34l);
                    activityBase.setCreateUserId(activityRoutine.getLoginId());
                    activityBase.setGmtCreate(new Date());
                    activityBase.setActivityName("会员生命周期管理");
                    activityBase.setActivityType(1);
                    activityBase.setStatus(0);
                    activityBase.setGroupId(activityRoutine.getGroupId());
                    ActivityBase activityBase1=activityBaseMapper.selectByRoutineId(activityBase.getActivityId());
                    if(activityBase1!=null){
                        activityBaseMapper.updateByPrimaryKeyActivityId(activityBase);
                    }else{
                        activityBaseMapper.insert(activityBase);
                    }

                }
                if (4==i){
                    ActivityRoutine activityRoutine1=new ActivityRoutine();
                    activityRoutine1.setActivityId(activityRoutine.getGroupId()*100+35l);
                    activityRoutine1.setActivityType(ActivityEnum.RoutineActivityType.MemberSigned.getType());
                    activityRoutine1.setGmtCreate(new Date());
                    activityRoutine1.setGmtModified(new Date());
                    activityRoutine1.setMemberRiented(1);//默认每个会员，(1,2)
                    activityRoutine1.setSendTime(0);
                    activityRoutine1.setGroupId(activityRoutine.getGroupId());
                    activityRoutineMapper.deleteByPrimaryKey(activityRoutine1.getActivityId());
                    activityRoutineMapper.insert(activityRoutine1);

                    ActivityBase activityBase=new ActivityBase();
                    activityBase.setActivityId(activityRoutine.getGroupId()*100+35l);
                    activityBase.setCreateUserId(activityRoutine.getLoginId());
                    activityBase.setGmtCreate(new Date());
                    activityBase.setActivityName("会员签到活动");
                    activityBase.setActivityType(1);
                    activityBase.setStatus(0);
                    activityBase.setGroupId(activityRoutine.getGroupId());
                    ActivityBase activityBase1=activityBaseMapper.selectByRoutineId(activityBase.getActivityId());
                    if(activityBase1!=null){
                        activityBaseMapper.updateByPrimaryKeyActivityId(activityBase);
                    }else{
                        activityBaseMapper.insert(activityBase);
                    }

                }

                if (5==i){
                    ActivityRoutine activityRoutine1=new ActivityRoutine();
                    activityRoutine1.setActivityId(activityRoutine.getGroupId()*100+36l);
                    activityRoutine1.setActivityType(ActivityEnum.RoutineActivityType.InviteRegistration.getType());
                    activityRoutine1.setGmtCreate(new Date());
                    activityRoutine1.setGmtModified(new Date());
                    activityRoutine1.setMemberRiented(1);//默认每个会员，(1,2)
                    activityRoutine1.setSendTime(0);
                    activityRoutine1.setGroupId(activityRoutine.getGroupId());
                    activityRoutineMapper.deleteByPrimaryKey(activityRoutine1.getActivityId());
                    activityRoutineMapper.insert(activityRoutine1);

                    ActivityBase activityBase=new ActivityBase();
                    activityBase.setActivityId(activityRoutine.getGroupId()*100+36l);
                    activityBase.setCreateUserId(activityRoutine.getLoginId());
                    activityBase.setGmtCreate(new Date());
                    activityBase.setActivityName("邀请注册福利活动");
                    activityBase.setActivityType(1);
                    activityBase.setStatus(0);
                    activityBase.setGroupId(activityRoutine.getGroupId());
                    ActivityBase activityBase1=activityBaseMapper.selectByRoutineId(activityBase.getActivityId());
                    if(activityBase1!=null){
                        activityBaseMapper.updateByPrimaryKeyActivityId(activityBase);
                    }else{
                        activityBaseMapper.insert(activityBase);
                    }

                }

                if (6==i){
                    ActivityRoutine activityRoutine1=new ActivityRoutine();
                    activityRoutine1.setActivityId(activityRoutine.getGroupId()*100+37l);
                    activityRoutine1.setActivityType(ActivityEnum.RoutineActivityType.ComsumeShare.getType());
                    activityRoutine1.setGmtCreate(new Date());
                    activityRoutine1.setGmtModified(new Date());
                    activityRoutine1.setMemberRiented(1);//默认每个会员，(1,2)
                    activityRoutine1.setSendTime(0);
                    activityRoutine1.setGroupId(activityRoutine.getGroupId());
                    activityRoutineMapper.deleteByPrimaryKey(activityRoutine1.getActivityId());
                    activityRoutineMapper.insert(activityRoutine1);

                    ActivityBase activityBase=new ActivityBase();
                    activityBase.setActivityId(activityRoutine.getGroupId()*100+37l);
                    activityBase.setCreateUserId(activityRoutine.getLoginId());
                    activityBase.setGmtCreate(new Date());
                    activityBase.setActivityName("消费分享红包活动");
                    activityBase.setActivityType(1);
                    activityBase.setStatus(0);
                    activityBase.setGroupId(activityRoutine.getGroupId());
                    ActivityBase activityBase1=activityBaseMapper.selectByRoutineId(activityBase.getActivityId());
                    if(activityBase1!=null){
                        activityBaseMapper.updateByPrimaryKeyActivityId(activityBase);
                    }else{
                        activityBaseMapper.insert(activityBase);
                    }

                }


            }

        }





        return null;
    }
}
